<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="layout :: common_header(~{::title})">
    <title >退货管理-拼多多</title>
</head>
<body th:replace="layout :: common_body(~{::div.layui-body})">
<div class="layui-body" style="padding:15px;">
    <style type="text/css">
        .T-box {
            width: 400px;
            padding: 0 20px;
            text-align: left;
        }

        .T-box p {
            position: relative;
            height: 36px;
            line-height: 36px;
            font-size: 14px;
            margin-bottom: 15px;
        }

        .T-box p span {
            display: inline-block;
            padding-right: 15px;
            text-align: right;
            width: 110px;
            line-height: 36px;
            font-size: 14px;
        }

        .T-box p input, .T-box p select {
            width: 160px;
            padding: 0 5px;
            height: 34px;
            line-height: 34px;
            font-size: 14px;
            border: 1px solid #d3d3d3;
            outline: none;
        }

        .T-box p i {
            padding: 0 10px;
            line-height: 36px;
            font-style: normal;
        }

        .T-box p b {
            display: inline-block;
            margin-left: 10px;
            cursor: pointer;
            width: 60px;
            height: 36px;
            line-height: 36px;
            background: #c33;
            color: #fff;
            text-align: center;
            font-size: 13px;
            font-weight: normal;
        }

        .T-result {
            margin-bottom: 0;
        }

        .T-result em {
            color: #c33;
            font-style: normal;
        }

      .update {
          display: inline-block;
          /*margin-left: 15px;*/
          /*padding: 0 20px;*/
          /*height: 40px;*/
          /*line-height: 40px;*/
          font-size: 14px;
          background: #1E9FFF;
          color: #fff;
          text-align: center;
          border-radius: 3px;
          cursor: pointer;
      }

        .update i {
            display: inline-block;
            margin-right: 5px;
            margin-left: 5px;
            vertical-align: middle;
            width: 30px;
            height: 30px;
            background: url();
        }
    </style>
    <div class="layui-tab layui-tab-brief" lay-filter="demo">
        <ul class="layui-tab-title" >

            
            <li th:classappend="${shippingStatus==1&&auditStatus==0}?'layui-this':''"><a th:href="@{/pdd/refund_list(shopId=${shopId},shippingStatus=1,auditStatus=0)}">待处理</a></li>
            <li th:classappend="${auditStatus==null}?'layui-this':''"><a th:href="@{/pdd/refund_list(shopId=${shopId})}">售后查询</a></li>
            <!-- <li><a th:href="@{/pdd/dashboard(shopId=${shopId})}">首页</a></li>
            <li><a th:href="@{/pdd/order_list(shopId=${shopId})}">订单管理</a></li>
            <li class="layui-this"><a th:href="@{/pdd/refund_list(shopId=${shopId})}">退货管理</a></li>
            <!-- <li><a th:href="@{/pdd/goods_list(shopId=${shopId})}">商品管理</a></li> -->
            <!-- <li ><a th:href="@{/pdd/sales_report(shopId=${shopId})}">销售统计</a></li>
            <li ><a th:href="@{/pdd/send_report(shopId=${shopId})}">发货统计</a></li> --> -->
        </ul>
    </div>
    <!-- content starts -->
<!--    <blockquote class="layui-elem-quote">售后订单</blockquote>-->

    <fieldset class="layui-elem-field">
        <legend>按条件搜索</legend>
        <div class="layui-field-box">
            <form class="layui-form layui-form-pane1" method="get" action="/pdd/refund_list">
                
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <input type="text" name="num" th:value="${num}" placeholder="退货ID、订单号、物流单号" class="layui-input" style="width: 200px;">
                    </div>
                    <div class="layui-inline">
                        <input type="text" name="goodsId" th:value="${goodsId}" placeholder="GoodsId" class="layui-input">
                    </div>
                    <!-- 
                    <div class="layui-inline">
                        <input type="text" name="logisticsCode" th:value="${logisticsCode}" placeholder="退货物流单号" class="layui-input">
                    </div> -->
                    <!-- <div class="layui-inline">
                        <label class="layui-form-label">申请时间</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" name="startTime" th:value="${startTime}" id="startTime" autocomplete="off" placeholder="yyyy-MM-dd HH:mm:ss">
                        </div>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" name="endTime" th:value="${endTime}" id="endTime" autocomplete="off" placeholder="yyyy-MM-dd  HH:mm:ss">
                        </div>
                    </div> -->
                <!-- </div>
                <div class="layui-form-item"> -->
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="shopId" id="shopId">
                                <option value="0">店铺</option>
                                <option  th:each="item: ${shops}" th:text="${item.name}" th:value="${item.id}">等待发货</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">

                        <select name="type" id="type">
                            <option value="-1">退货类型</option>
                            <option value="2">仅退款</option>
                            <option value="3">退货退款</option>
                            <option value="4">换货</option>
                            <option value="9">已发货仅退款</option>
                            
                        </select>

                    </div>
                    <div class="layui-inline">

                        <select name="auditStatus" id="auditStatus">
                            <option value="-1">全部处理状态</option>
                            <option value="0">待处理</option>
                            <option value="2">已签收</option>
                            <option value="9">供应商已退款</option>
                            <option value="10">换货已完成</option>
                            <option value="91">等待财务处理</option>
                            <option value="99">已关闭</option>
                            <option value="100">计入损失</option>
                        </select>

                    </div>
                    <div class="layui-inline">

                            <select name="status" id="status">
                                <option value="">退货状态</option>
                                <option th:each="s:${T(com.b2c.entity.pdd.EnumPddOrderRefundStatus).values()}" th:text="${s.getName()}" th:value="${s.getPddIndex()}" >售后状态</option>
                            </select>

                    </div>
                    <div class="layui-inline">

                        <select name="shippingStatus" id="shippingStatus">
                            <option value="">订单发货状态</option>
                            <option value="0">未发货</option>
                            <option value="1">已发货</option>
                        </select>

                    </div>
                    
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <button type="submit" class="layui-btn layui-btn-primary">搜索</button>
                        <i>总共：<span style="color: red" th:text="${totalSize}"></span>条记录</i>
                    </div>
                    <div class="layui-inline">
                        <span id="upd_order" class="layui-btn-xs layui-btn-radius layui-btn-primary">拉取新退款</span>
                        <span id="upd_order_" style="display: none;" class="update layui-btn-xs layui-btn-radius layui-btn-disabled"><i></i>更新中</span>
                    </div>

                    <div class="layui-inline" >
                        <span id="upd_order_status_all" class="layui-btn-xs layui-btn-radius layui-btn-primary">更新待处理退货订单状态</span>
                        <span id="upd_order_status_all_" style="display: none;" class="update layui-btn-xs layui-btn-radius layui-btn-primary"><i></i>更新中</span>
                        <!-- <span class="layui-btn layui-btn-warm upd_order_by_num"><i></i>更新指定订单</span> -->
                        <!--                        <a id="upd_order_status_all" style="display: none">更新当前订单状态</a>-->
                    </div>

                </div>



            </form>
            <div class="layui-form-item">

            </div>
        </div>
    </fieldset>

    <!-- start dd-wrapper -->
    <link rel="stylesheet" href="/goods/order.css"/>
    <div class="dd-wrapper">
        <table class="dd-table">
            <tr>
                <th  width="180px">信息</th>
                <th width="18%">商品</th>
                
                <th>金额</th>
                <th>发货状态</th>
                <th>售后类型</th>
                <th  width="200px">售后理由</th>
    
                <th>退货物流</th>
                <th>售后状态</th>
                <th width="150px">ERP状态</th>
                <th width="180px">操作</th>
            </tr>
            <tr th:each="item:${list}">
             
                <td >
                    <p th:text="${item.id}"></p>
                    <p th:text="${item.order_sn}" style="color: rgba(140, 136, 139, 0.933);"></p>
                    <p th:text="${#dates.format(item.created_time * 1000, 'yyyy-MM-dd HH:mm:ss')}" style="color: rgba(198, 197, 198, 0.933);"></p>
                    
                </td>
                <td>
                    <img th:src="${item.goods_image}" width="50px" height="50px" style="float: left;" />
                    <p style="float: left;padding-left: 10px;">
                        <span th:text="${#strings.abbreviate(item.erpGoodsName,18)}" th:title="${item.goods_name}"></span>
                        <span  th:if="${#strings.isEmpty(item.erpGoodsName)}" style="color:#9393f9;" th:text="${item.goodsId}"></span>
                        <br/>
                        <span th:text="${item.GoodsSpec}"></span> x <span th:text="${item.quantity}"></span><br/>
                        GoodsId：<span th:text="${item.goodsId}"></span>
                        
                    </p>
                </td>
                <td>
                    实收<span th:text="${item.order_amount}"></span><br/>
                    退款<span th:text="${item.refund_amount}"></span>
                </td>
        
                <td >
                    <span th:if="${item.orderStatus == 1}" style="color: #4b4b4b">未发货</span>
                    <span th:if="${item.orderStatus > 1}" >已发货</span><br />
                    shipping:<span th:text="${item.shippingStatus}"></span>
                </td>
                <td >
                    <span th:if="${item.after_sales_type == 3}" style="color: #0000FF">退货退款</span>
                    <span th:if="${item.after_sales_type == 2}" style="color: #009E94">仅退款</span>
                    <span th:if="${item.after_sales_type == 4}" style="color: #cf971c">换货</span>
                    <span th:if="${item.after_sales_type == 9}" style="color: #009E94">拦截退货</span>
                </td>
                <td >
                    <span th:text="${item.after_sale_reason}"></span><br />
                    <span th:text="${item.remark}" style="color: rgb(106, 0, 255)"></span><br/>
                    
                </td>
        
                
                <td>
                    <p th:text="${item.tracking_company}"></p>
                    <p th:text="${item.tracking_number}"></p>
                </td>
                <td>
                    <span th:text="${T(com.b2c.entity.pdd.EnumPddOrderRefundStatus).getName(item.after_sales_status)}"></span><br/>
                    <span th:text="${item.updated_time}" title="更新时间"></span><br />
                    
                    <a th:attr="data-id=${item.getId()}" th:if="${item.after_sales_status != 10 &&item.after_sales_status != 9  &&item.after_sales_status != 12 &&item.after_sales_status != 11 }" 
                    class="upd-status layui-btn-xs layui-btn-primary layui-btn-radius" style="color: rgb(111, 106, 106)" href="javascript:;" title="更新退货状态">
                        <i class="layui-icon layui-icon-refresh"></i>
                    </a>
                </td>
                <td>
                    <span th:if="${item.auditStatus==0}" style="color: rgb(21, 205, 222);">未处理</span>
                    <span th:if="${item.auditStatus==2}" style="color: rgb(37, 204, 45);">已签收</span>
                    <span th:if="${item.auditStatus==9}" style="color: red;">供应商已退款</span>
                    <span th:if="${item.auditStatus==10}" style="color: red;">换货已完成</span>
                    <span th:if="${item.auditStatus==99}" style="color: rgba(90, 90, 90, 0.771);">已关闭</span>
                    <span th:if="${item.auditStatus==91}" style="color: rgba(90, 90, 90, 0.771);">等待财务处理</span>
                    <span th:if="${item.auditStatus==100}" style="color: rgba(90, 90, 90, 0.771);">计入损失</span> 
                    
                    <a title="修改状态" class="edit_status"  th:attr="data-id=${item.getId()},orderStatus=${item.orderStatus}" href="javascript:;">
                        <i class="layui-icon layui-icon-edit"></i>
                    </a>
                    <p th:text="${item.auditTime}"></p>
                    <p th:text="${T(com.b2c.entity.enums.erp.EnumOrderReturnStatus).getName(item.orderReturnStatus)}"></p>
                </td>
                <td class="center">
                    <a th:href="@{/pdd/refund_detail(id=${item.getId()},shopId=${shopId})}">详情</a>
                    <a th:attr="refund-id=${item.getId()},describe=${item.describe}" href="javascript:;" title="备注" class="sellerRemark">
                       添加备注
                    </a>
          
                     <a th:attr="refund-id=${item.getId()},describe=${item.describe},type=${item.after_sales_type},trackcode=${item.tracking_number},shopId=${item.shopId},goodsName=${item.erpGoodsName}" href="javascript:;" 
                     title="加入代办列表" class="addtodo">
                        加入代办
                    </a>
                    <br />
                  
                    <a th:if="${item.auditStatus==0  }" class="layui-btn-sm layui-btn-primary layui-btn-radius sign_refund"  th:attr="data-id=${item.getId()},auditStatus=2" href="javascript:;">签收</a>

                    <a th:if="${item.auditStatus==2  }" title="标记为已退款" class="layui-btn-sm layui-btn-primary layui-btn-radius sign_refund"  th:attr="data-id=${item.getId()},auditStatus=9" href="javascript:;">
                        标记退款
                    </a>

                    <br/>
                    <span th:text="${item.describe}" th:if="${#strings.isEmpty(item.describe)==false}"  style="color: rgb(240, 240, 240);border: #adadff solid 1px;background-color: rgb(43, 143, 231);"></span>
                   

                </td>
            </tr>
        </table>
    </div>
    <!-- end dd-wrapper -->
    <div class="layui-box layui-laypage layui-laypage-default pageBox">
        <paging:pager th:value="${pageIndex}" th:rows="${totalSize}" th:size="${pageSize}"/>
    </div>

    <script type="text/template" id="tuikuan">
        <div class="T-box">
            <p><span>订单编号</span><i id="ddbh"></i></p>
            <p><span>退货编号</span><i id="thbh"></i></p>
            <p><span>付款时间</span><i id="fksj"></i></p>
            <p><span>退款时间</span><i id="tksj"></i></p>
            <p><span>订单总额</span><i id="ddze"></i></p>
            <p><span>收款人</span><i id="skr"></i></p>
            <p><span>退款金额</span><i id="skje"></i></p>
            <p><span>退款方式</span><i>按原账户退回</i></p>
        </div>
    </script>

    <script type="text/template" id="send-goods">
        <div class="T-box">
            <p><span>快递公司</span>
                <select id="send-company">
                    <option value="">请选择快递公司</option>
                    <option th:each="item:${company}" th:value="${item.getCode()}" th:text="${item.getName()}"></option>
                </select>
            </p>
            <p><span>快递单号</span>
                <input type="text" autocomplete="off" name="express_code"/>
            </p>
<!--            <p><span>发件人地址</span><input type="text" name="express_address"/></p>-->
            <p class="T-result"><span>&nbsp;</span><em id="erro"></em></p>
        </div>
    </script>

    <script type="text/template" id="edit_status">
        <div class="T-box">
            <p><span>状态</span>
                <select id="new-status">
                    <option value="0">请选择状态</option>
                    <option value="9">供应商已退款</option>
                    <option value="10">换货已完成</option>
                    <option value="99" selected>已关闭</option>
                    <option value="91" >等待财务处理</option>
                    <option value="100" >计入损失</option>
                </select>
            </p>
            <p><span>备注</span>
                <input type="text" autocomplete="off" value="未发货" id="new-remark" name="new-remark"/>
            </p>
            <p class="T-result"><span>&nbsp;</span><em id="erro"></em></p>
        </div>
    </script>

    <script src="/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="/goods/Tips/Tips.min.js"></script>
    <script type="text/javascript">
        layui.use(['laydate','form'], function () {
            var laydate = layui.laydate;
            var form = layui.form;
            laydate.render({
                elem: '#startTime',
                type: 'datetime'
            });
            laydate.render({
                elem: '#endTime',
                type: 'datetime'
            });


        $(function () {
            $("#type").val('[[${type}]]');
            $("#auditStatus").val('[[${auditStatus}]]');
            $("#status").val('[[${status}]]');
            $("#shopId").val('[[${shopId}]]');
            $('#shippingStatus').val('[[${shippingStatus}]]')
            form.render('select'); //刷新select选择框渲染

            if($("#type").val()==3){
                $("#status_menu li:nth-child(1)").addClass("layui-this");
                $("#status_menu li:nth-child(2)").removeClass("layui-this");
            }else{
                $("#status_menu li:nth-child(2)").addClass("layui-this");
                $("#status_menu li:nth-child(1)").removeClass("layui-this");
            }

            //备注
            $(".sellerRemark").click(function(){
                var refundId = $(this).attr('refund-id');
                var describe = $(this).attr('describe');
                var obj = $(this).siblings('b').eq(0);

                layer.prompt({
                    formType: 2,
                    value: describe,
                    title: '请输入备注'
                }, function(value, index, elem){
                    var data = {refundId:refundId, describe:value};
                    $.ajax({
                        url: "/ajax_pdd/refund_describe_updata",
                        type: "POST",
                        dataType: "JSON",
                        contentType: 'application/json',
                        data:JSON.stringify(data),
                        success: function (res) {

                            if(res.code == 0){

                                $(obj).text(value);
                                layer.close(index)
                                layer.msg(res.msg);
                                // location=location;
                            }else
                                layer.alert(res.msg);
                        }
                    });


                });
            });
            
            //加入代办
            $(".addtodo").click(function(){
                var refundId = $(this).attr('refund-id');
                var shopId = $(this).attr('shopId');
                var type = $(this).attr('type');
                var typeName = '';
                if(type== 3|| type==2) typeName= '退货';
                else if(type==4) typeName = '换货';
                else if(type==9) typeName ='退回';
                var describe = $(this).attr('describe');
                if(describe == ''||describe==null) describe = "退货";
                var trackcode = $(this).attr('trackcode');
                if(trackcode == undefined) trackcode='';
                else trackcode += "";
                var goodsName = $(this).attr('goodsName');
                var obj = $(this).siblings('b').eq(0);

                layer.prompt({
                    formType: 2,
                    value: '拼多多'+typeName+'：'+trackcode+"（"+goodsName+"）",
                    title: '代办内容'
                }, function(value, index, elem){
                    var data = {sourceType:1,sourceId:refundId,shopId:shopId, content:value};
                    $.ajax({
                        url: "/ajax_todo/add_todo",
                        type: "POST",
                        dataType: "JSON",
                        contentType: 'application/json',
                        data:JSON.stringify(data),
                        success: function (res) {

                            if(res.code == 0){

                                $(obj).text(value);
                                layer.close(index)
                                layer.msg(res.msg);
                                // location=location;
                            }else
                                layer.alert(res.msg);
                        }
                    });


                });
            });


            //标记为已处理
            $(".sign_refund").click(function () {
                var $this = $(this);
                let refId = $(this).attr('data-id');
                let auditStatus = $(this).attr('auditStatus');
                

                if(confirm("该操作不可逆，是否确定要标记为已处理？")){
                    var data = {
                        id: refId,
                        auditStatus:auditStatus
                    };
                    $.ajax({
                        url: "/ajax_pdd/sign_refund",
                        type: "POST",
                        dataType: 'json',
                        contentType: 'application/json',
                        data: JSON.stringify(data),
                        success: function (res) {
                            if (res.code == 0) {
                               layer.msg("处理成功");
                                //$this.parent().parent().remove();
                               window.location.reload();
                            } else {
                                alert(res.msg);
                                return false;
                            }
                        }
                    });
                }

            })

            //修改状态edit_status
            $('.edit_status').click(function (e) {
                e.stopPropagation();
                e.preventDefault();
                var id = $(this).attr('data-id');
                var orderStatus = $(this).attr('orderStatus');
                var $obj = $(this).parent().parent();
                
                // var remark = '';
                // if(orderStatus == 1) {
                //     remark="未发货"; 
                //     $("#new-remark").val('未发货');
                //     $("#new-remark").text('未发货');
                //     $("#new-status").val(99);
                // }
                Tips.confirm({
                    title: '修改处理状态',
                    content: $('#edit_status').html(),
                    define: function () {
                        var data = {
                            id: id,
                            auditStatus: $("#new-status  option:selected").val(),
                            remark: $("input[name='new-remark']").val()
                        };
                        $.ajax({
                            url: "/ajax_pdd/sign_refund",
                            type: "POST",
                            dataType: 'json',
                            contentType: 'application/json',
                            data: JSON.stringify(data),
                            success: function (res) {
                                if (res.code == 0) {
                                    layer.msg("成功");
                                    $obj.remove();
                                    // Tips.alert('成功', function () {
                                    //     location.reload(true);
                                    // });
                                } else {
                                    Tips.alert(res.msg);
                                }
                            }
                        });
                    }
                });
            });

            //更新退款订单
            $("#upd_order").click(function () {
                
                let startTime = $("#startTime").val();
                let endTime = $("#endTime").val();
                var shopId = $("#shopId").val();
                if(shopId==0||shopId == ''||shopId =='null'||shopId==null) {alert("请选择店铺");return;}

                $("#upd_order").hide();
                $("#upd_order_").show();
                let data = {
                    startTime:startTime,
                    endTime:endTime,
                    shopId:shopId
                };

                $.ajax({
                    url: "/ajax_pdd/pull_refund",
                    type: "POST",
                    dataType: "JSON",
                    contentType: 'application/json',
                    data:JSON.stringify(data),
                    success: function (res) {
                        if (res.code == 0) {
                            Tips.alert({
                                    title: '更新订单',
                                    content: '更新结果：'+res.msg+'【'+JSON.stringify(res.data )+'】',
                                    define: function () {
                                        window.location.reload();//.href = "/pdd/refund_list?shopId="+shopId;
                                    }
                                });
                            // layer.alert("更新成功！更新结果："+JSON.stringify(res.data))
                            // window.location.reload();
                            // location.href = "/refund/list";
                        }else if(res.code==601){
                            alert(res.msg);
                            location.href = "/pdd/oauth?shopId="+shopId;
                        }else alert(res.msg);
                        $("#upd_order").show();
                        $("#upd_order_").hide();
                        // else {
                        //     alert(res.msg);
                        // }
                    }
                });

            });

            //单个更新状态
            $(".upd-status").click(function () {
                var curr = $(this);
                var loading = $(this).next('.upd-status-loading');
                var refundId = $(this).attr("data-id");
                var shopId = $("#shopId").val();
                if(shopId==0) {alert("请选择店铺");return;}

                $(curr).hide();
                $(loading).show();
                $.ajax({
                    url: "/ajax_pdd/upd_refund_by_id",
                    type: "POST",
                    dataType: "JSON",
                    contentType: 'application/json',
                    data:JSON.stringify({id:refundId,shopId:shopId}),
                    success: function (res) {
                        layer.msg(res.msg);
                        if (res.code == 0) {
                            // location.href = "/refund/list";
                            window.location.reload();
                        }else if(res.code==601){
                            location.href = "/pdd/oauth?shopId="+shopId;
                        }
                        $(loading).hide();
                        $(curr).show();
                        // else {
                        //     alert(res.msg);
                        // }
                    }
                });
            })

            //更新当前订单状态
            $("#upd_order_status_all").click(function () {
                $("#upd_order_status_all").hide();
                $("#upd_order_status_all_").show();
                var shopId = $("#shopId").val();
                if(shopId==0) {alert("请选择店铺");return;}
                let data = {
                    shopId: shopId,
                    auditStatus:$("#auditStatus").val()
                };

                $.ajax({
                    url: "/ajax_pdd/upd_refund_by_audit",
                    type: "POST",
                    dataType: "JSON",
                    contentType: 'application/json',
                    data:JSON.stringify(data),
                    success: function (res) {
                        if (res.code == 0) {
                            layer.msg("订单状态更新成功:"+res.msg );
                            //window.location.reload();
                            // Tips.alert({
                            //     title: '更新订单',
                            //     content: '订单状态更新成功',
                            //     define: function () {
                            //         location.href = "/pdd/orderList?shopId="+shopId;
                            //     }
                            // });
                        }else if(res.code==601){
                            // $("#upd_order").show();
                            // $("#upd_order_").hide();
                            alert(res.msg);
                            // Tips.alert(res.msg);
                            location.href = "/pdd/oauth?shopId="+shopId;
                        }
                        else {
                            Tips.alert(res.msg);
                        }
                        $("#upd_order_status_all").show();
                        $("#upd_order_status_all_").hide();
                    }
                })
            });

        
        });
        });
    </script>
</div>
</body>
</html>
